package br.os.communication;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
/**
 * CommReceberOS
 * Classe responsável por Receber as ordens de serviço 
 * do servidor
 */
public class CommConsultarOS extends Thread{
	
	private String tecnico;
	private CommNotificador notificador;
	private String url;

	/**
	 * @param url
	 * @param tecnico 
	 * @param notificador
	 */
	public CommConsultarOS(String url, String tecnico,CommNotificador notificador) {
		this.url = url;
		this.tecnico = tecnico;
		this.notificador = notificador;
	}

	/**
	 * Envia uma requisicao de autenticacao ao Servidor.
	 */
	public void run() {
		try {
			HttpConnection httpConnection = (HttpConnection) Connector.open(url);
			httpConnection.setRequestMethod(HttpConnection.POST);
			httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.1 Configuration/CLDC-1.1");
			httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			String encoded = "tecnico=" + tecnico;
			httpConnection.setRequestProperty("Content-Length", Integer.toString(encoded.length()));
			httpConnection.openOutputStream().write(encoded.getBytes());
			notificador.notificarInputStream(httpConnection.openInputStream());
		} catch (Exception e) {
			notificador.notificarException(e);
		}
	}

}
